<?php
global $_GPC, $_W;
$uniacid = $_W['uniacid'];
$op = $_GPC['op'];
$res = $this->res;
$plus = $this->plus;
load()->model('mc');
$setting = $this->baseset($uniacid);
$fltype = $setting['fltype'];
$jiangli = $setting['ddjiangli'] / 100;
$jiangli2 = $setting['ddjiangli2'] / 100;
if ($fltype == 1) {
	$ddbili = intval($setting['ddbili']);
} else {
	$ddbili = intval($setting['ddbili']) / 100;
}
$openid = $_GPC['openid'];
$orderid = $this->strsafe($_GPC['orderid']);
$user = pdo_fetch("SELECT * FROM " . tablename($this->tableuser) . " WHERE uniacid = '{$_W['uniacid']}' AND openid = '{$openid}'");
if (!$user) {
	exit(json_encode(array('status' => '500')));
}
if ($_W['ispost'] && $op == 'add') {
	$order = pdo_fetch('SELECT * FROM ' . tablename($this->tableorderus) . ' WHERE uniacid = :uniacid AND orderid = :orderid', array(':uniacid' => $_W['uniacid'], ':orderid' => $orderid));
	if ($order) {
		exit(json_encode(array('status' => '0')));
	}
	$data = array('uniacid' => $_W['uniacid'], 'openid' => $user['openid'], 'avatar' => $user['avatar'], 'nickname' => $user['nickname'], 'orderid' => $orderid, 'ctime' => time(),);
	if ($setting['ddisstatus'] == 1) {
		$data['status'] = 0;
	} else {
		$thisorder = pdo_fetch("SELECT * FROM " . tablename($this->tableorder) . " WHERE uniacid = '{$_W['uniacid']}' AND orderid = '{$orderid}'");
		if ($thisorder['orderstatus'] == "订单结算") {
			$data['queren'] = 1;
			$data['status'] = 1;
			if ($fltype == 1) {
				$data['jifen'] = $thisorder['shouru_yongjin'] * $ddbili;
			} else {
				$data['flfee'] = round($thisorder['shouru_yongjin'] * $ddbili, 2);
			}
		} else {
			$data['status'] = 0;
		}
	}
	if (empty($order)) {
		pdo_insert($this->tableorderus, $data);
		$insertid = pdo_insertid();
		if ($insertid) {
			if ($setting['ddisstatus'] == 0 && $thisorder['orderstatus'] == "订单结算") {
				$poster = pdo_fetch("SELECT * FROM " . tablename('bsht_tbk_poster') . " WHERE uniacid = :uniacid order by id asc", array(':uniacid' => $_W['uniacid']));
				$showuser = pdo_fetch('SELECT * FROM ' . tablename($this->tableorderus) . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $insertid));
				$postuser1 = pdo_fetch('SELECT * FROM ' . tablename($this->tableuser) . ' WHERE uniacid = :uniacid AND openid = :openid', array(':uniacid' => $_W['uniacid'], ':openid' => $showuser['openid']));
				$postuser2 = pdo_fetch('SELECT * FROM ' . tablename($this->tableuser) . ' WHERE uniacid = :uniacid AND uid=:uid', array(':uniacid' => $_W['uniacid'], ':uid' => $postuser1['helpid']));
				if ($fltype == 1) {
					$rejifen = $showuser['jifen'];
					$rejifen2 = $showuser['jifen'] * $jiangli;
					$rejifen3 = $showuser['jifen'] * $jiangli2;
				} else {
					$rejifen = round($showuser['flfee'], 2);
					$rejifen2 = round($showuser['flfee'] * $jiangli, 2);
					$rejifen2 = round($showuser['flfee'] * $jiangli2, 2);
				}
				$toopenid = $postuser1['openid'];
				$touid = mc_openid2uid($toopenid);
				if ($fltype == 1) {
					$status = mc_credit_update($touid, 'credit1', $rejifen, array($touid, '审核订单,奖励【' . $rejifen . '】积分'));
				} else {
					$status = mc_credit_update($touid, 'credit2', $rejifen, array($touid, '审核订单,奖励【' . $rejifen . '】余额'));
				}
				$mcinfo = mc_fetch($touid);
				$mc_data = array('credit1' => $mcinfo['credit1'], 'credit2' => $mcinfo['credit2'],);
				pdo_update($this->tableuser, $mc_data, array('uid' => $touid, 'uniacid' => $_W['uniacid']));
				if ($poster['cscore'] > 0) {
					$touid1 = $postuser1['helpid'];
					if ($touid1 != 0) {
						if ($fltype == 1) {
							$status1 = mc_credit_update($touid1, 'credit1', $rejifen2, array($touid1, '被邀请人审核订单,奖励【' . $rejifen2 . '】积分'));
						} else {
							$status1 = mc_credit_update($touid1, 'credit2', $rejifen2, array($touid1, '被邀请人审核订单,奖励【' . $rejifen2 . '】余额'));
						}
						$mcinfo = mc_fetch($touid1);
						$mc_data = array('credit1' => $mcinfo['credit1'], 'credit2' => $mcinfo['credit2'],);
						pdo_update($this->tableuser, $mc_data, array('uid' => $touid1, 'uniacid' => $_W['uniacid']));
					}
				}
				if ($poster['pscore'] > 0) {
					$touid2 = $postuser2['helpid'];
					if ($touid2 != 0) {
						if ($fltype == 1) {
							$status2 = mc_credit_update($touid2, 'credit1', $rejifen3, array($touid2, '被邀请人审核订单,奖励【' . $rejifen3 . '】积分'));
						} else {
							$status2 = mc_credit_update($touid2, 'credit2', $rejifen3, array($touid2, '被邀请人审核订单,奖励【' . $rejifen3 . '】余额'));
						}
						$mcinfo = mc_fetch($touid2);
						$mc_data = array('credit1' => $mcinfo['credit1'], 'credit2' => $mcinfo['credit2'],);
						pdo_update($this->tableuser, $mc_data, array('uid' => $touid2, 'uniacid' => $_W['uniacid']));
					}
				}
				$this->temp_ms_sh($showuser['openid'], "订单审核，奖励" . $rejifen . "积分", $settings['sd_mshead'], $settings['sd_msfoot'], $settings['sd_msdo'], $settings['sd_msmain']);
			}
			$this->temp_ms_gl('用户提交订单', '有用户提交订单，请尽快审核！', 'uc_dd');
			if ($setting['ddisstatus'] == 0 && $thisorder['orderstatus'] == "订单结算") {
				exit(json_encode(array('status' => '1', 'jifen' => $rejifen)));
			} else {
				exit(json_encode(array('status' => '1')));
			}
		} else {
			exit(json_encode(array('status' => '500')));
		}
	} else {
		exit(json_encode(array('status' => '0')));
	}
}